package com.avic.modules.equipment.rest;

import com.avic.dozer.service.IGenerator;
import com.avic.modules.equipment.domain.Equipment;
import com.avic.modules.equipment.domain.EquipmentRepair;
import com.avic.modules.equipment.service.EquipmentRepairService;
import com.avic.modules.equipment.service.dto.EquipmentRepairDto;
import com.avic.modules.equipment.service.dto.EquipmentRepairQueryCriteria;
import com.avic.modules.equipment.service.mapper.EquipmentRepairMapper;
import com.avic.modules.log.domain.vo.LogEnumeration;
import com.avic.modules.log.domain.vo.LogVo;
import com.avic.modules.log.service.LogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author zhangzikang
 * @date 2022-08-14
 */
@Api(tags = "设备管理: 设备维修接口")
@RestController
@RequestMapping("/api/equipment/repair")
@AllArgsConstructor
public class EquipmentRepairController {

    private final IGenerator generator;
    private final EquipmentRepairService repairService;
    private final LogsService logsService;
    private final EquipmentRepairMapper equipmentRepairMapper;

    /**
     * 设备维修信息
     * @param criteria
     * @return
     */
    @ApiOperation("设备维修信息")
    @GetMapping
    @PreAuthorize("@el.check('admin','repair:list')")
    public ResponseEntity<Object> getEquipment(EquipmentRepairQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(repairService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    /**
     * 导出设备维修信息
     * @param response
     * @param criteria
     */
    @ApiOperation("导出设备维修信息")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','repair:download')")
    public void download(HttpServletResponse response, EquipmentRepairQueryCriteria criteria, Pageable pageable) throws IOException {

        repairService.download(generator.convert(repairService.queryAlls(criteria,pageable), EquipmentRepairDto.class), response);
    }

    /**
     * 添加设备维修信息
     * @param equipment
     * @return
     */
    @ApiOperation("添加设备维修信息")
    @PostMapping
    @PreAuthorize("@el.check('admin','repair:add')")
    public ResponseEntity<Object> addEquipment(@Validated @RequestBody EquipmentRepair equipment){
        repairService.addEquipment(equipment);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 修改设备维修信息
     * @param equipment
     * @return
     */
    @ApiOperation("修改设备维修信息")
    @PutMapping
    @PreAuthorize("@el.check('admin','repair:edit')")
    public ResponseEntity<Object> editEquipment(@Validated @RequestBody EquipmentRepair equipment){
        repairService.editEquipment(equipment);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除设备维修信息
     * @param ids
     * @return
     */
    @ApiOperation("删除设备维修信息")
    @DeleteMapping
    @PreAuthorize("@el.check('admin','repair:del')")
    public ResponseEntity<Object> delEquipment(@RequestBody Set<String> ids){
        List<String> strings = new ArrayList<>();
        for (String id : ids) {
            EquipmentRepair equip = equipmentRepairMapper.selectById(id);
            strings.add(equip.getEquipName());
        }
        // 创建操作日志
        LogVo logVo = new LogVo();
        String name = "[删除]了"+ " [" + strings.toString() + "] " +"设备维修记录信息";
        logVo.setBehavior(name);
        logVo.setAction(LogEnumeration.Delete.getMessage());
        logsService.save(logVo);
        repairService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
